<template>
  <div class="subjects">
    <!-- 新增按钮 -->
<el-row>
  <el-col :span="24"><div class="grid-content bg-purple-dark">
     <el-button type="primary" size="mini" @click="add()" >新增科目</el-button>
    </div></el-col>
</el-row>
<!-- 条查 -->
<el-row>
  <el-col :span="24"><div class="grid-content bg-purple-dark">
        <el-form :inline="true" :model="form" class="demo-form-inline">
  <el-form-item label="关键词" >
    <el-input v-model="form.subjectName" placeholder="请输入内容" size="mini"></el-input>
  </el-form-item>
    <el-form-item label="收支类型">
    <el-select v-model="form.status" placeholder="请选择" size="mini">
      <el-option label="收入科目" value="2"></el-option>
      <el-option label="支出科目" value="1"></el-option>
    </el-select>
  </el-form-item>
    <el-button type="primary" @click="search()" size="mini">查询</el-button>
    <el-button type="primary" @click="Reset()" size="mini">重置</el-button>
  </el-form-item>
</el-form>
    </div></el-col>
</el-row>
<!-- 表格 -->
<el-row>
  <el-col :span="24"><div class="grid-content bg-purple-dark">
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="subjectName" label="科目名称" width="auto">
      </el-table-column>
      <el-table-column prop="status" label="收支类型" width="auto">
        <template slot-scope="scope">
<span v-if="scope.row.status=='1'">支出</span>
<span v-else >收入</span>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="状态" width="auto">
      </el-table-column>
      <el-table-column label="操作" width="300px">
                <template slot-scope="scope">
            <el-button size="mini" @click="handleDelete(scope.$index, scope.row)"
      >删除</el-button>
                </template>
              </el-table-column>
    </el-table>
             <!-- 分页 -->
                    <el-pagination
                      @size-change="handleSizeChange"
                      @current-change="handleCurrentChange"
                      :current-page="pageNum"
                      :page-sizes="[2, 5, 10, 20]"
                      :page-size="pageSize"
                      layout="total, sizes, prev, pager, next, jumper"
                      :total="total"
                    >
                    </el-pagination>
    </div></el-col>
</el-row>
 <!-- 新增弹框 -->
         <el-dialog title = "新增" :visible.sync="dialogVisible" width="40%">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="科目名称">
          <el-input
            v-model="formInline.subjectName"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="收支类型">
    <el-select v-model="formInline.status" placeholder="请选择" size="mini">
      <el-option label="收入科目" value="2"></el-option>
      <el-option label="支出科目" value="1"></el-option>
    </el-select>
  </el-form-item>
        <el-form-item label="备注">
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="formInline.remark"
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addlist()" >新增</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "subjects",
  data() {
    return {
      dialogVisible: false,
      //分页
      pageNum: 1,
      pageSize: 5,
      total: 0,
      tableData: [],
      //条查
      form: {
        subjectName: "",
        status: "",
      },
      //新增
      formInline: {
        subjectName: "",
        status: "",
        remark: "",
      },
    };
  },
  created() {
    this.search();
  },
  methods: {
    //新增
    addlist() {
      this.$http.post("Subject/addSubject", this.formInline).then((res) => {
        this.formInline = res.data;
        if (res.data.code == 200) {
          this.$message({
            message: "恭喜你，" + res.data.msg,
            type: "success",
          });
          this.dialogVisible = false;
          this.search();
        } else {
          this.$message.error("错了哦，" + res.data.msg);
        }
      });
    },
    //条查 分页
    search() {
      this.$http
        .post(
          "Subject/findSubjectPagination?pageNum=" +
            this.pageNum +
            "&pageSize=" +
            this.pageSize,
          this.form
        )
        .then((res) => {
          this.tableData = res.data.rows;
          this.total = res.data.total;
        });
    },
    //分页
    handleCurrentChange(val) {
      this.pageNum = val;
      this.search();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.search();
    },
    //删除
    handleDelete(index, row) {
      this.$http.get("Subject/deleteSubject?id=" + row.id).then((res) => {
        console.log(res);
        if (res.data.code == 200) {
          this.$message({
            message: "恭喜你，删除成功",
            type: "success",
          });
          this.search();
        } else {
          this.$message.error("错了哦，删除失败");
        }
      });
    },
    //重置
    Reset() {
      (this.form.subjectName = ""), (this.form.status = ""), this.search();
    },
    //新增弹框
    add() {
      this.dialogVisible = true;
    },
  },
};
</script>

<style scoped>
</style>